package top.tangyh.lamp.care.mapper;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.ibatis.annotations.Param;
import top.tangyh.basic.base.mapper.SuperMapper;
import top.tangyh.lamp.care.entity.BObsvRecord;
import org.springframework.stereotype.Repository;
import top.tangyh.lamp.care.vo.query.UpdateStatusDTO;
import top.tangyh.lamp.common.constant.CommonConstant;
import top.tangyh.lamp.common.utils.PDateUtils;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * Mapper 接口
 * 观察项记录表
 * </p>
 *
 * @author zqb
 * @date 2024-07-24 15:47:49
 * @create [2024-07-24 15:47:49] [zqb] [代码生成器生成]
 */
@Repository
public interface BObsvRecordMapper extends SuperMapper<BObsvRecord> {

    /**
     * 体温单-血压 模块数据
     */
    List<BObsvRecord> getBloodPressureListData(String hospitalNumber, String startTime, String endTime);

    /**
     * 体温单-呼吸 模块数据
     */
    List<BObsvRecord> getTemperatureTOfBreathingListData(String hospitalNumber, String startTime, String endTime);
    /**
     * 体温单-体温 模块数据
     */
    List<BObsvRecord> getTemperaListData(String hospitalNumber, Long startTime, Long endTime);

    /**
     * 体温单-大便 模块数据
     */
    List<BObsvRecord> getShitListData(String hospitalNumber, String startTime, String endTime);

    Integer getUseDays(@Param("patientIdent") String patientIdent,
                       @Param("startTime") Long startTime,
                       @Param("endTime") Long endTime,
                       @Param("obsvCodeList") List<String> obsvCodeList,
                       @Param("isDev") Integer isDev);


    default void deleteByCodesAndTime(String patientIdent, String hospitalNumber, Long recTime, Set<String> codes){
        LambdaQueryWrapper<BObsvRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StrUtil.isNotEmpty(patientIdent),BObsvRecord::getPatientIdent,patientIdent)
                .eq(StrUtil.isNotEmpty(hospitalNumber),BObsvRecord::getHospitalNumber,hospitalNumber)
                .eq(BObsvRecord::getRecTime,recTime)
                .in(BObsvRecord::getObsvCode,codes)
                .eq(BObsvRecord::getIsDeleted, CommonConstant.NO_NUMBER);
        delete(queryWrapper);
        return;
    }

    default void updateStatus(UpdateStatusDTO updateStatusDTO) {
        String hospitalNumber = updateStatusDTO.getHospitalNumber();
        List<Long> recTimeList = updateStatusDTO.getRecTimeList();
        Integer deleteStatus = updateStatusDTO.getDeleteStatus();
        LambdaQueryWrapper<BObsvRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BObsvRecord::getHospitalNumber, hospitalNumber)
                .in(BObsvRecord::getRecTime, recTimeList);
        BObsvRecord BObsvRecord = new BObsvRecord();
        BObsvRecord.setIsDeleted(deleteStatus);
        this.update(BObsvRecord, queryWrapper);
    }

    default void deleteData(String patientIdent, String hospitalNumber, LocalDateTime timePoint) {
        LambdaQueryWrapper<BObsvRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BObsvRecord::getPatientIdent,patientIdent).eq(BObsvRecord::getHospitalNumber,hospitalNumber)
                .eq(BObsvRecord::getRecTime, PDateUtils.getTimestampOfDateTime(timePoint));
        delete(queryWrapper);
    }

   

    /**
     *  查询指定班次范围内最新记录的采集人名称
     * @param patientIdent
     * @param startTime
     * @param endTime
     * @return
     */
    default String selectCaijirenByTime(String patientIdent,Long startTime,Long endTime){
        LambdaQueryWrapper<BObsvRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BObsvRecord::getPatientIdent,patientIdent)
                .ge(BObsvRecord::getRecTime,startTime)
                .le(BObsvRecord::getRecTime,endTime)
                .isNotNull(BObsvRecord::getCaijiren)
//				.eq(BObsvRecord::getCreateSource,"dev")
                .orderByDesc(BObsvRecord::getRecTime).last("limit 1");;
        BObsvRecord obsvRecord = selectOne(queryWrapper);
        if(null!=obsvRecord){
            return obsvRecord.getCaijiren();
        }
        else{
            return "";
        }
    }


    default void deleteByPatAndCodeAndTime(String patientIdent,Set<String> codes, Set<Long> times,String account){
        LambdaQueryWrapper<BObsvRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BObsvRecord::getPatientIdent,patientIdent)
                .in(BObsvRecord::getObsvCode,codes)
                .in(BObsvRecord::getRecTime,times)
                .eq(BObsvRecord::getIsDeleted,CommonConstant.NO_NUMBER)
                .and(wapper->wapper.or().eq(BObsvRecord::getCaijirenGh,account).or().isNull(BObsvRecord::getCaijirenGh));
        delete(queryWrapper);
    }

    /**
     * 体温单-脉搏心率 模块数据
     */
    List<BObsvRecord> getPulseHeartRateListData(String hospitalNumber, String startTime, String endTime);
}


